package com.marver.common.dao;

import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageRowBounds;
import com.marver.common.result.Page;
import com.marver.common.result.Result;
import com.marver.common.result.ResultStatus;
import com.marver.entity.ByPage;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;

import java.util.List;
import java.util.Map;

public abstract class BaseDAOImpl extends SqlSessionDaoSupport implements BaseDAO {

//	@Autowired
//	public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
//		super.setSqlSessionTemplate(sqlSessionTemplate);
//	}
	
	@Autowired
	@Override
	public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
		super.setSqlSessionFactory(sqlSessionFactory);
	}

	@Override
	public void addBySqlMap(String statementName, Object obj)
			throws DataAccessException {
		getSqlSession().insert(statementName, obj); 
	}

	@Override
	public <T> void  batchAddBySqlMap(String statementName, List<T> list)
			throws DataAccessException {
		if(list!=null){
			for(T obj:list){
				getSqlSession().insert(statementName, obj); 
			}
		}
	}
	
	@Override
	public void addBySqlMap(String statementName, Map<String, Object> map)
			throws DataAccessException {
		getSqlSession().insert(statementName, map); 
	}

	@Override
	public int deleteBySqlMap(String statementName, Map<String, Object> map)
			throws DataAccessException {
		return getSqlSession().delete(statementName, map); 
	}

	@Override
	public int deleteBySqlMap(String statementName, Object obj)
			throws DataAccessException {
		return getSqlSession().delete(statementName, obj); 
	}

	@Override
	public int updateBySqlMap(String statementName, Object obj)
			throws DataAccessException {
		return getSqlSession().update(statementName, obj); 
	}

	@Override
	public int updateBySqlMap(String statementName, Map<String, Object> map)
			throws DataAccessException {
		return getSqlSession().update(statementName, map); 
	}

	@Override
	public Object findObjectBySqlMap(String statementName, Map<String, Object> map) {
		return getSqlSession().selectOne(statementName, map); 
	}

	@Override
	public Object findObjectBySqlMap(String statementName, Object obj) {
		return getSqlSession().selectOne(statementName, obj); 
	}

	@Override
	public List listByPageBySqlMap(String statementName, Map<String, Object> map,int skipResults, int pageSize) {
		return getSqlSession().selectList(statementName, map, new PageRowBounds(skipResults, pageSize));  
	}

	@Override
	public List listByPageBySqlMap(String statementName, Object obj,
			int skipResults, int pageSize) {
		return getSqlSession().selectList(statementName, obj, new PageRowBounds(skipResults, pageSize));  
	}

	@Override
	public List listBySqlMap(String statementName, Map<String, Object> map) {
		return getSqlSession().selectList(statementName, map);  
	}

	@Override
	public List listBySqlMap(String statementName, Object obj) {
		return getSqlSession().selectList(statementName, obj);  
	}

	@Override
	public Result listByPageBySqlMap(String statementName, Object obj) {
		//pageNum/pageSize
		if(ByPage.class.isAssignableFrom(obj.getClass())) {
			List list=getSqlSession().selectList(statementName, obj);
			PageInfo p = new PageInfo(list);
			Page page = new Page();
			BeanUtils.copyProperties(p, page);
			return new Result(ResultStatus.SUCCESS.getErrorCode(), list, page);
		}else {
			throw new RuntimeException("此方法第二个参数必须继承ByPage");
		}
	}
}
